package com.realife.eson.task.coordinate.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wums
 * http://localhost:9025/{context-path}/swagger-ui.html
 * http://localhost:9025/doc.html
 *
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Bean
    public Docket api() {
        List<Parameter> pars = new ArrayList<Parameter>();
        //添加header token信息
        //header中的ticket参数非必填，传空也可以
        ParameterBuilder typePar = new ParameterBuilder();
        typePar.name("Content-Type").description("Content-Type")
                .modelRef(new ModelRef("string")).parameterType("header").defaultValue("application/json;charset=UTF-8")
                .required(false).build();
        pars.add(typePar.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enableSwagger)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
//                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("eson-task-coordinate接口文档")
                .description("eson-task-coordinate接口文档")
                .version("1.0.0-SNAPSHOT")
                .contact(new Contact("wums", "", "wums@realife.com"))
                .build();
    }
}
